﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace UDS.Components
{
    /// <summary>
    /// Project 类
    /// </summary>
    public class ProjectClass
    {
        #region 返回项目相关信息

        /// <summary>
        /// 返回项目相关信息
        /// </summary>
        public SqlDataReader GetClassInfo(int classID)
        {
            var data = new Database();
            var parameters = new[] { Database.MakeInParam("@TeamID", SqlDbType.Int, 20, classID) };

            try
            {
                return data.RunDataReader("dbo.sproc_GetTeamInfo", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("项目信息读取出错!", ex);
            }
        }

        #endregion

        #region 返回子项目相关信息

        /// <summary>
        /// 返回子项目相关信息
        /// </summary>
        public SqlDataReader GetSubClassInfo(int classID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[] { Database.MakeInParam("@ClassID", SqlDbType.Int, 20, classID) };

            try
            {
                return data.RunDataReader("dbo.sproc_GetSubClass", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("子项目信息读取出错!", ex);
            }
        }

        #endregion

        #region 返回某项目下是否存在子项目

        /// <summary>
        /// 返回某项目下是否存在子项目
        /// </summary>
        public bool IsExistSubClass(int classID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[] { Database.MakeInParam("@Class_id", SqlDbType.Int, 20, classID) };

            try
            {
                using (SqlDataReader dr = data.RunDataReader("dbo.sproc_GetAllChildClassID_V2", parameters))
                {
                    if (dr.Read())
                    {
                        return true;
                    }
                }

                return false;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("子项目信息读取出错!", ex);
            }
        }

        #endregion

#if false
        #region 返回项目成员相关信息

        /// <summary>
        /// 返回项目成员相关信息
        /// </summary>
        public SqlDataReader GetMemberInClass(int classID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[] { Database.MakeInParam("@ClassID", SqlDbType.Int, 20, classID) };

            try
            {
                return data.RunDataReader("dbo.sproc_GetMemberInClass", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("项目成员信息读取出错!", ex);
            }
        }

        #endregion
#endif

        #region 返回上级项目负责人相关信息

        /// <summary>
        /// 返回上级项目负责人相关信息
        /// </summary>
        public SqlDataReader GetParentLeader(int classID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            const int RightCode = 10; //缺省值
            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[] {
                Database.MakeInParam("@TeamID", SqlDbType.Int, 20, classID),
                Database.MakeInParam("@RightCode", SqlDbType.Int, 20, RightCode)
            };

            try
            {
                return data.RunDataReader("dbo.sproc_GetParentLeader", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("项目上级负责人信息读取出错!", ex);
            }
        }

        #endregion

        #region 返回项目负责人相关信息

        /// <summary>
        /// 返回项目负责人相关信息
        /// </summary>
        public SqlDataReader GetLeader(int classID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[] { Database.MakeInParam("@TeamID", SqlDbType.Int, 20, classID) };

            try
            {
                return data.RunDataReader("dbo.sproc_GetLeader", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("项目负责人信息读取出错!", ex);
            }
        }

        #endregion

        #region 项目移动

        /// <summary>
        /// 项目移动
        /// </summary>
        public void Remove(int souID, int desID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[] {
                Database.MakeInParam("@FromTeamID", SqlDbType.Int, 20, souID),
                Database.MakeInParam("@ToTeamID", SqlDbType.NVarChar, 20, desID)
            };

            try
            {
                data.RunProc("dbo.sproc_MoveTeam", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("项目移动出错", ex);
            }
        }

        #endregion

        #region 项目复制

        /// <summary>
        /// 项目复制
        /// </summary>
        /// <param name="souID">源项目id</param>
        /// <param name="desID">目标项目id</param>
        /// <param name="operatorman">操作者</param>
        public void Copy(int souID, int desID, string operatorman)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[] {
                Database.MakeInParam("@FromTeamID", SqlDbType.Int, 20, souID),
                Database.MakeInParam("@ToTeamID", SqlDbType.NVarChar, 20, desID),
                Database.MakeInParam("@operator", SqlDbType.NVarChar, 50, operatorman)
            };

            try
            {
                data.RunProc("dbo.sproc_CopyTeam", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("项目移动出错", ex);
            }
        }

        #endregion

        #region 项目添加

        /// <summary>
        /// 项目添加
        /// </summary>
        public void Add(int ParentID, string ProjectName, string ProjectRemark, string Wright, int Status, int Scale, DateTime StartDate, DateTime EndDate)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[] {
                Database.MakeInParam("@ParentID", SqlDbType.Int, 20, ParentID),
                Database.MakeInParam("@ProjectName", SqlDbType.NVarChar, 20, ProjectName.Trim()),
                Database.MakeInParam("@ProjectRemark", SqlDbType.NVarChar, 400, ProjectRemark.Trim()),
                Database.MakeInParam("@Wright", SqlDbType.NVarChar, 40, Wright),
                Database.MakeInParam("@Status", SqlDbType.Int, 40, Status),
                Database.MakeInParam("@Scale", SqlDbType.Int, 20, Scale),
                Database.MakeInParam("@StartDate", SqlDbType.DateTime, 30, StartDate),
                Database.MakeInParam("@EndDate", SqlDbType.DateTime, 30, EndDate)
            };

            try
            {
                data.RunProc("dbo.sproc_AddProject", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("项目添加出错", ex);
            }
        }

        #endregion

        #region 项目订阅

        /// <summary>
        /// 项目订阅
        /// </summary>
        public void Subscribe(string Username, int ClassID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[] {
                Database.MakeInParam("@StaffName", SqlDbType.NVarChar, 50, Username),
                Database.MakeInParam("@ClassID", SqlDbType.Int, 10, ClassID)
            };

            try
            {
                data.RunProc("dbo.sproc_SubscibeProject", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("项目订阅出错", ex);
            }
        }

        #endregion

        #region 获取某项目名称

        /// <summary>
        /// 获取某项目名称
        /// </summary>
        /// <param name="ClassID">项目ID</param>
        public static string GetProjectName(int ClassID)
        {
            string ProjectName = "";
            var data = new Database();
            var parameters = new[] { Database.MakeInParam("@projectid", SqlDbType.Int, 20, ClassID) };
            try
            {
                using (SqlDataReader dr = data.RunDataReader("dbo.sproc_GetProjectName", parameters))
                {
                    if (dr.Read())
                    {
                        ProjectName = dr[0].ToString();
                    }
                    return ProjectName;
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                return "";
            }
        }

        #endregion

        #region 获取某项目详细信息

        /// <summary>
        /// 获取某项目详细信息
        /// </summary>
        /// <param name="ClassID">项目ID</param>
        public SqlDataReader GetProjectDetail(int ClassID)
        {
            var data = new Database();
            var parameters = new[] { Database.MakeInParam("@TeamID", SqlDbType.Int, 20, ClassID) };
            try
            {
                return data.RunDataReader("dbo.sproc_GetTeamInfo", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                return null;
            }
        }

        #endregion

        #region 修改某项目信息

        /// <summary>
        /// 修改某项目详细信息
        /// </summary>
        public void Revise(int ClassID, string ProjectName, string ProjectRemark, int Status, int Scale, DateTime StartDate, DateTime EndDate)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@ProjectID", SqlDbType.Int, 20, ClassID),
                Database.MakeInParam("@ProjectName", SqlDbType.NVarChar, 20, ProjectName.Trim()),
                Database.MakeInParam("@ProjectRemark", SqlDbType.NVarChar, 400, ProjectRemark.Trim()),
                Database.MakeInParam("@Status", SqlDbType.Int, 40, Status),
                Database.MakeInParam("@Scale", SqlDbType.Int, 20, Scale),
                Database.MakeInParam("@StartDate", SqlDbType.DateTime, 30, StartDate),
                Database.MakeInParam("@EndDate", SqlDbType.DateTime, 30, EndDate)
            };

            try
            {
                data.RunProc("dbo.sproc_UpdateProject", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
            }
        }

        #endregion

        #region 删除某项目

        /// <summary>
        /// 删除某项目
        /// </summary>
        /// <param name="ClassID">项目ID</param>
        public void Delete(int ClassID)
        {
            var data = new Database();
            var parameters = new[] {
                Database.MakeInParam("@TeamID", SqlDbType.Int, 20, ClassID),
            };

            try
            {
                data.RunProc("dbo.sproc_DeleteTeam", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
            }
        }

        #endregion

        #region 把字符串年月日后面的时间去除

        /// <summary>
        /// 把字符串年月日后面的时间去除
        /// </summary>
        public static string changeString(string ss)
        {
            string lkk = "";
            for (int i = ss.Length; i > 0; i--)
            {
                char lk = ss[i - 1];
                if (lk == ' ')
                {
                    lkk = ss.Substring(0, i);
                    break;
                }
            }
            return lkk;
        }

        #endregion
    }
}